package com.souku.finance.application.ui.more;

import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.volley.manager.RequestManager;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.souku.finance.application.R;
import com.souku.finance.application.adapter.NormalRequestAdapter;
import com.souku.finance.application.base.BaseActivity;
import com.souku.finance.application.bean.RequestNormalBean;
import com.souku.finance.application.http.UrlConstact;
import com.souku.finance.application.utils.JsonUtil;
import com.souku.finance.application.utils.ParamUtil;
import com.souku.finance.application.utils.ToastUtil;
import com.souku.finance.application.http.volley.ReqListener;
import com.souku.finance.application.http.volley.SuccessError;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Android on 2016/12/20.
 */

public class RequestNormalActivity extends BaseActivity implements View.OnClickListener {

    protected RelativeLayout topBack;
    protected TextView topTitle;
    protected PullToRefreshListView pullList;
    private int page = 1;
    private List<RequestNormalBean.ListBean> list = new ArrayList<>();
    private NormalRequestAdapter adapter;

    @Override
    public void internet() {
        pullList.setRefreshing(true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        pullList.setRefreshing(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        super.setContentView(R.layout.act_requestnormal);
        initView();
        adapter = new NormalRequestAdapter(list);
        pullList.setAdapter(adapter);
        pullList.setMode(PullToRefreshBase.Mode.BOTH);
        pullList.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                list.clear();
                page = 1;
                getInvitationinfo();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                page++;
                getInvitationinfo();
            }
        });
        topTitle.setText("邀请记录");
    }

    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.top_back) {
            finish();
        }
    }

    private void initView() {
        topBack = (RelativeLayout) findViewById(R.id.top_back);
        topBack.setOnClickListener(RequestNormalActivity.this);
        topTitle = (TextView) findViewById(R.id.top_title);
        pullList = (PullToRefreshListView) findViewById(R.id.pull_list);
    }

    private void getInvitationinfo() {
        HashMap<String, String> map = ParamUtil.getParams();
        map.put("pg_no", page + "");
        RequestManager.getInstance().post(UrlConstact.INVITATION_INFO, ParamUtil.getSing(map), new ReqListener(this, new SuccessError() {

            @Override
            public void onSuccess(String response) {
                pullList.onRefreshComplete();
                if (JsonUtil.getCode(response) == 1) {
                    RequestNormalBean bean = JsonUtil.fastBean(response, RequestNormalBean.class);
//                    JsonUtil.fastArrayBean(JsonUtil.getData(response).toString(),RequestNormalBean.class);
                    if (bean.getList().size() == 0) {
                        ToastUtil.show("没有更多数据");
                    } else {
                        list.addAll(bean.getList());
                        adapter.notifyDataSetChanged();
                    }
                } else {
                    ToastUtil.show(JsonUtil.getMessage(response));
                }
            }

            @Override
            public void onError(String errorMsg) {
                pullList.onRefreshComplete();
                ToastUtil.show("请求失败");
            }
        }), ParamUtil.getHeaders());
    }
}
